﻿using AutoMapper;
using Max.PH5_2011A.Domain;
using Max.PH5_2011A.DTO;
using Max.PH5_2011A.IRespository;
using Max.PH5_2011A.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2011A.Service
{
    public class MemberInfoService : BaseService<MemberInfoModel>, IMemberInfoService
    {
        IMemberInfoRespository _respository;
        IMapper _map;
        public MemberInfoService(IMemberInfoRespository respository, IUnitOfWork unitOfWork, IMapper map) : base(respository, unitOfWork)
        {
            _respository = respository;
            _map = map;
        }

        public Tuple<ResponseStateCode, string, MemberInfoOutputDto> Login(string username, string password)
        {
            var m = _respository.Query().Where(x => x.LoginName.Equals(username)).FirstOrDefault();
            if (m == null)
            {
                //用户名不存在
                return Tuple.Create<ResponseStateCode, string, MemberInfoOutputDto>(ResponseStateCode.USERNAMEERROR, "用户名不存在", null);
            }

            //MD5加密
            password = MD5Hash.Hash.Content(password);

            if (!m.Password.Equals(password))
            {
                //密码不正确
                return Tuple.Create<ResponseStateCode, string, MemberInfoOutputDto>( ResponseStateCode.USERPASSWORDERROR, "密码不正确", null);
            }

            return Tuple.Create<ResponseStateCode, string, MemberInfoOutputDto>(
                 ResponseStateCode.SUCCESS, "登录成功", _map.Map<MemberInfoModel,MemberInfoOutputDto>(m)   );

        }

    }
}
